package edu.cuit.avatar;

import java.io.UnsupportedEncodingException;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/9 11:14
 */
public class StringDemo {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String message = "this is a test message";
        /*
         *   replace和replaceAll的区别
         *      都是替换全部
         *      replaceAll支持模式匹配(正则表达式),replace不支持,必须史明确的子串
         */
        System.out.println(message.replace("is", "**")); //全部替换
        System.out.println(message.replaceAll("is", "**"));//全部替换
        System.out.println(message);

        //替换第一次出现的子串
        System.out.println(message.replaceFirst("is", "**"));

        System.out.println("java".equals("java"));//true
        System.out.println("java".matches("java"));//true

        System.out.println("java is fun".matches("java.*")); //true
        System.out.println("java is cool".matches("java.*"));//true

        /*
         * 匹配手机号
         *      1. 必须以1开头
         *      2. 第二位必须是3,5,8中的一位
         *      3. 必须是11位的数字
         */
        String regex = "^1[358]\\d{9}$";
        System.out.println("110".matches(regex)); //false
        System.out.println("15800001111".matches(regex)); //true
        System.out.println("1580000111".matches(regex)); //false
        System.out.println("17800001111".matches(regex)); //false


        String info = "java@web@spring";
        String[] words = info.split("@");
        for(String s : words)
            System.out.println(s);

        //将字符串转为字符数组
        char[] chars = info.toCharArray();
        System.out.println(chars.length);

        String s = new String(chars);
        System.out.println(s);
        System.out.println(new String(chars, 0, 5));
        //将字符串转成字节数组
        info.getBytes();
        //按照指定的字符编码进行转换
        info.getBytes("utf-8");

        /*
         * %d   %5d
         * %f   %5.2f   %.2f
         * %b   %5b
         * %c
         * %s
         */
        String format = String.format("%5d - %2d = %d",1000, 999, 1000 - 9999);
        System.out.println(format);
    }
}
